package com.epoint.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.epoint.common.Result;
import com.epoint.entity.QuoteInfo;
import com.epoint.service.QuoteInfoService;

/**
 * Servlet implementation class QuoteInfoAction
 */
@WebServlet("/quoteinfoaction")
public class QuoteInfoAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
    QuoteInfoService quoteInfoService = new QuoteInfoService();
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public QuoteInfoAction() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String method = request.getParameter("method");
		
		if("".equals(method)) {
			// TODO
		}else {
			String pageIndex = request.getParameter("pageIndex");
			String pageSize = request.getParameter("pageSize");
			String sortField = request.getParameter("sortField");
			String sortOrder = request.getParameter("sortOrder");
			
			List<QuoteInfo> productList = quoteInfoService.getQuoteList(Integer.valueOf(pageIndex), Integer.valueOf(pageSize),sortField, sortOrder);
			int count = quoteInfoService.getQuoteListCount();
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("total", count);
			resultMap.put("data", productList);
			// 返回构建的json数据
			out.println(JSONObject.toJSONStringWithDateFormat(resultMap, "yyyy-MM-dd"));
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		PrintWriter out = response.getWriter();
		
		if("del".equals(method)) {
			// 删除报价信息
			String supplierId = request.getParameter("supplierId");
			String productId = request.getParameter("productId");
			int res = quoteInfoService.deleteQuoteInfoById(supplierId,productId);
			
			String returnCode = Result.returnCode(res);
			out.print(returnCode);
		}else if("add".equals(method)) {
			String data = request.getParameter("data");
			QuoteInfo quoteInfo = JSONObject.parseObject(data,QuoteInfo.class);
			
			int res = quoteInfoService.addQuoteInfo(quoteInfo);
			
			String returnCode = Result.returnCode(res);
			out.print(returnCode);
		}
	}

}
